// ========================================================================================================================
// base64编解码器：用于api请求或响应前对象数据进行编解码操作
// ========================================================================================================================

var Base64 = require('../lib/base64.min.js');
var myDebugger = require('../utils/myDebugger.js');

// 微信用户信息对象
var WxInfoVO = {
    encode: function (wxInfoVO) {
        if (!wxInfoVO) return null
        if (wxInfoVO.NickName) {
            try {
                wxInfoVO.NickName = Base64.encode(wxInfoVO.NickName)
            } catch (error) {
                myDebugger.error('base64 encode error: wxInfoVO.NickName')
            }
        }
        return wxInfoVO
    },
    decode: function (wxInfoVO) {
        if (!wxInfoVO) return null
        if (wxInfoVO.NickName) {
            try {
                wxInfoVO.NickName = Base64.decode(wxInfoVO.NickName)
            } catch (error) {
                myDebugger.error('base64 decode error: wxInfoVO.NickName')
            }
        }
        return wxInfoVO
    }
};

// 角色对象
var RoleVO = {
    encode: function (roleVO) {
        if (!roleVO) return null
        if (roleVO.Name) {
            try {
                roleVO.Name = Base64.encode(roleVO.Name)
            } catch (error) {
                myDebugger.error('base64 encode error: roleVO')
            }
        }
        return roleVO
    },

    decode: function (roleVO) {
        if (!roleVO) return null
        try {
            roleVO.Name = Base64.decode(roleVO.Name)
        } catch (error) {
            myDebugger.error('base64 decode error: roleVO')
        }
        return roleVO
    }
};

// 角色对象列表
var RoleListVO = {
    encode: function (roleListVO) {
        if (!roleListVO) return null
        for (var i in roleListVO) {
            roleListVO[i] = RoleVO.encode(roleListVO[i]);
        }
        return roleListVO
    },

    decode: function (roleListVO) {
        if (!roleListVO) return null
        for (var i in roleListVO) {
            roleListVO[i] = RoleVO.decode(roleListVO[i]);
        }
        return roleListVO
    }
};

// 段落内容
var AContentVO = {
    encode: function (aContentVO) {
        if (!aContentVO) return null
        var contents = aContentVO.Contents;
        for (var i in contents) {
            if (contents[i]) {
                try {
                    contents[i] = Base64.encode(contents[i])
                } catch (error) {
                    myDebugger.error('base64 encode error: aContents[' + i + ']')
                }
            }
        }
        return aContentVO
    },

    decode: function (aContentVO) {
        if (!aContentVO) return null
        var contents = aContentVO.Contents;
        for (var i in contents) {
            try {
                contents[i] = Base64.decode(contents[i])
            } catch (error) {
                myDebugger.error('base64 decode error: aContents[' + i + ']')
            }
        }
        return aContentVO
    }
};

// 段落列表内容
var AContentListVO = {
    encode: function (aContentListVO) {
        if (!aContentListVO) return null
        for (var i in aContentListVO) {
            aContentListVO[i] = AContentVO.encode(aContentListVO[i]);
        }
        return aContentListVO
    },

    decode: function (aContentListVO) {
        if (!aContentListVO) return null
        for (var i in aContentListVO) {
            aContentListVO[i] = AContentVO.decode(aContentListVO[i]);
        }
        return aContentListVO
    }
};

// 对话内容：封面 + 摘要 + 段落 + 角色列表 等
var TalkBasicVO = {
    // 编码
    encode: function (talkBasicVO) {
        if (!talkBasicVO) return null
        if (talkBasicVO.Title) {
            try {
                talkBasicVO.Title = Base64.encode(talkBasicVO.Title)
            } catch (error) {
                myDebugger.error('base64 encode error: talkBasicVO.Title')
            }
        }
        if (talkBasicVO.Desc) {
            try {
                talkBasicVO.Desc = Base64.encode(talkBasicVO.Desc)
            } catch (error) {
                myDebugger.error('base64 encode error: talkBasicVO.Desc')
            }
        }
        talkBasicVO.Section = AContentListVO.encode(talkBasicVO.Section);
        talkBasicVO.Roles = RoleListVO.encode(talkBasicVO.Roles);
        if (talkBasicVO.Tags && talkBasicVO.Tags[0]) {
            try {
                talkBasicVO.Tags[0] = Base64.encode(talkBasicVO.Tags[0])
            } catch (error) {
                myDebugger.error('base64 encode error: talkBasicVO.Tags[0]')
            }
        }

        return talkBasicVO;
    },

    // 解码
    decode: function (talkBasicVO) {
        if (!talkBasicVO) return null
        if (talkBasicVO.Title) {
            try {
                talkBasicVO.Title = Base64.decode(talkBasicVO.Title)
            } catch (error) {
                myDebugger.error('base64 decode error: talkBasicVO.Title')
            }
        }
        if (talkBasicVO.Desc) {
            try {
                talkBasicVO.Desc = Base64.decode(talkBasicVO.Desc)
            } catch (error) {
                myDebugger.error('base64 decode error: talkBasicVO.Desc')
            }
        }
        talkBasicVO.Section = AContentListVO.decode(talkBasicVO.Section);
        talkBasicVO.Roles = RoleListVO.decode(talkBasicVO.Roles);
        if (talkBasicVO.Tags && talkBasicVO.Tags[0]) {
            try {
                talkBasicVO.Tags[0] = Base64.decode(talkBasicVO.Tags[0])
            } catch (error) {
                myDebugger.error('base64 decode error: talkBasicVO.Tags[0]')
            }
        }
        return talkBasicVO;
    }
};

// 服务器用户信息对象
var UserVO = {
    encode: function (userVO) {
        if (!userVO) return null
        if (userVO.ShowName) {
            try {
                userVO.ShowName = Base64.encode(userVO.ShowName)
            } catch (error) {
                myDebugger.error('base64 encode error: userVO')
            }
        }
        if (userVO.WxInfo) {
            userVO.WxInfo = WxInfoVO.encode(userVO.WxInfo)
        }
        return userVO
    },

    decode: function (userVO) {
        if (!userVO) return null
        try {
            userVO.ShowName = Base64.decode(userVO.ShowName)
        } catch (error) {
            myDebugger.error('base64 decode error: userVO', error)
        }
        if (userVO.WxInfo) {
            userVO.WxInfo = WxInfoVO.decode(userVO.WxInfo)
        }
        return userVO
    }
};

// 用户列表
var UserListVO = {
    encode: function (userListVO) {
        if (!userListVO) return null
        for (var i in userListVO) {
            userListVO[i] = UserVO.encode(userListVO[i]);
        }
        return userListVO
    },

    decode: function (userListVO) {
        if (!userListVO) return null
        for (var i in userListVO) {
            userListVO[i] = UserVO.decode(userListVO[i]);
        }
        return userListVO
    }
};

// 对话对象（详情）：对话内容 + 作者信息 + 状态等
var TalkVO = {
    encode: function (talkVO) {
        if (!talkVO) return null
        talkVO.User = UserVO.encode(talkVO.User);
        talkVO.PublishItem = TalkBasicVO.encode(talkVO.PublishItem);
        talkVO.DraftItem = TalkBasicVO.encode(talkVO.DraftItem);
        return talkVO;
    },

    decode: function (talkVO) {
        if (!talkVO) return null
        talkVO.User = UserVO.decode(talkVO.User);
        talkVO.WxInfo = WxInfoVO.decode(talkVO.WxInfo);
        talkVO.PublishItem = TalkBasicVO.decode(talkVO.PublishItem);
        talkVO.DraftItem = TalkBasicVO.decode(talkVO.DraftItem);
        return talkVO;
    }
};

// 对话列表
var TalkListVO = {
    // encode: function (talkListVO) {
    // },

    decode: function (talkListVO) {
        if (!talkListVO) return null
        for (var i in talkListVO) {
            talkListVO[i] = TalkVO.decode(talkListVO[i]);
        }
        return talkListVO
    }
};

// 评论内容
var CommentVO = {
    encode: function (commentVO) {
        if (!commentVO) return null
        // 摘要
        if (commentVO.Content) {
            try {
                commentVO.Content = Base64.encode(commentVO.Content)
            } catch (e) {
                myDebugger.error('base64 encode error：（评论内容）')
            }
        }
        return commentVO;
    },

    decode: function (commentVO) {
        if (!commentVO) return null
        try {
            if (commentVO.Content) {
                try {
                    commentVO.Content = Base64.decode(commentVO.Content)
                } catch (error) {
                    myDebugger.error('base64 decode error: (评论内容)')
                }
            }
            if (commentVO.User) {
                commentVO.User = UserVO.decode(commentVO.User)
            }
            if (commentVO.Children && commentVO.Children.length > 0) {
                for (var i in commentVO.Children) {
                    if (commentVO.Children[i]) {
                        commentVO.Children[i] = CommentVO.decode(commentVO.Children[i])
                    }
                }
            }
        } catch (e) {
            myDebugger.error('base64 decode error：（评论内容）')
        }
        return commentVO;
    }
};

// 评论列表
var CommentListVO = {
    decode: function (commentListVO) {
        if (!commentListVO) return
        for (var i in commentListVO) {
            commentListVO[i] = CommentVO.decode(commentListVO[i])
        }
        return commentListVO;
    }
};

module.exports = {
    RoleVO: RoleVO,
    RoleListVO: RoleListVO,

    UserVO: UserVO,
    UserListVO: UserListVO,

    TalkVO: TalkVO,
    TalkListVO: TalkListVO,

    CommentVO: CommentVO,
    CommentListVO: CommentListVO
}